import {
	mapState,
	mapActions
} from 'vuex';
import config from "@/utils/config.js";
import {
		browser
	} from '@/common/ua.js'
export default {
	data() {
		return {

		}
	},
	computed: {
		...mapState(['isLogin']),
	},
	methods: {
		...mapActions(['setToken', 'setUserinfo']),
		// 登录
		login() {
			
			let code =  window.location.search.split("code=")[1]
			console.log(code)
			if(code == undefined){
				return
			}else{
				code = code.split("&")[0]
			}
			this.$Net.post(this.$api.H5Authorization, {
				code: code
			}, 1, '授权中').then(res => {
				if (res.body) {
					let {
						authorization,
						userInfo
					} = res.body
					this.setToken(authorization)
					if (userInfo) {
						this.setUserinfo(JSON.parse(userInfo))
					}
					// console.log(location.)
					location.href = location.origin+location.pathname+location.hash
					uni.showToast({
						icon: 'none',
						title: '授权成功'
					})
				}else{
					uni.showToast({
						icon: 'none',
						title: '授权失败'
					})
				}
			}).catch(er => {
				uni.showToast({
					icon: 'none',
					title: '授权失败'
				})
				// 去掉code
				if(code){
					location.href = location.origin+location.pathname+location.hash
					// setTimeout(()=>{
					// 	window.location.href = window.location.origin + window.location.pathname
					// },1000)
				}
			})
		},
		// 点击授权方法
		getUserInfo() {
			console.log('进入授权')
			if (browser.versions.weixin) { // 微信公众号中
				let Appid = config.APPID //"wx1c2578d18d27abd7"; //为测试号id
				// 回调地址 
				let local = encodeURIComponent(window.location.href);
				let url =
					`https://open.weixin.qq.com/connect/oauth2/authorize?appid=${Appid}&redirect_uri=${local}&response_type=code&scope=snsapi_userinfo&state=2#wechat_redirect`
				window.location.href = url
			} else {
				uni.showToast({
					title: '当前客户端不支持此操作！请在微信中打开',
					icon: 'none'
				})
			}
		},
	}
}
